/* eslint-disable import/no-dynamic-require */
/* eslint-disable no-unused-vars */
/* eslint-disable import/no-extraneous-dependencies */
const path = require('path');
const webpack = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');
const fs = require('fs');

const packageJson = require(path.resolve(__dirname, 'package.json'));
let cesiumSource = 'node_modules/cesium/Source';
if (!fs.existsSync(`${cesiumSource}/Cesium.js`)) {
  cesiumSource = '../../node_modules/cesium/Source';
}
const cesiumWorkers = '../Build/Cesium/Workers';
module.exports = {
  custom: {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        src: path.resolve(__dirname, './src'),
        cesium$: 'cesium/Cesium',
        cesium: 'cesium/Source',
      },
    },
    plugins: [
      new webpack.DefinePlugin({
        CESIUM_BASE_URL: JSON.stringify(
          process.env.PUBLIC_URL
            ? `${process.env.PUBLIC_URL}${packageJson.name}/${packageJson.version}/static/`
            : 'static/'
        ),
      }),
      new CopyPlugin([
        {
          from: path.join(cesiumSource, cesiumWorkers),
          to: 'static/Workers',
        },
        {
          from: path.join(cesiumSource, 'Assets'),
          to: 'static/Assets',
        },
        {
          from: path.join(cesiumSource, 'Widgets'),
          to: 'static/Widgets',
        },
      ]),
    ],
  },
  override: (config) => {
    config.module.rules[2].oneOf[2].options.babelrc = true;
    config.module.rules[2].oneOf[2].options.configFile = true;
    config.module.unknownContextCritical = false;
    if (process.env.NODE_ENV === 'development' && !process.env.WIDGET_PATH) {
      require('@jkyu/monet-studio-reqeust')();
    }
    return config;
  },
};
